package com.hrhx.springboot.scheduled;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.context.annotation.Import;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hrhx.springboot.config.RestTemplateConfig;
import com.hrhx.springboot.utils.JsonUtil;

@Component
@Import(value = {RestTemplateConfig.class})
public class DataScheduledTask{
	
	@Resource
    private SimpMessagingTemplate simpMessagingTemplate; 

	@Scheduled(cron="0/2 * *  * * ? ")   //每2秒执行一次
	public void getLineDataScheduledTask(){
		Random random = new Random();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String,String> map = new HashMap<String,String>();
		map.put("最低气温",String.valueOf(random.nextInt(10)));
		map.put("最高气温",String.valueOf(random.nextInt(10)));
		map.put("xxx",sdf.format(date));
		simpMessagingTemplate.convertAndSend("/topic/line/data", new JsonUtil().toStrings(map));    
		System.out.println("我是DataScheduledTask,getLineDataScheduledTask()正在发送后台推送数据："+new JsonUtil().toStrings(map));
		
	}
	
	@Scheduled(cron="0/2 * *  * * ? ")   //每2秒执行一次
	public void getGaugeDataScheduledTask(){	
		Random random = new Random();
		Double d = random.nextDouble()*100;
		DecimalFormat df = new DecimalFormat("######0.00");
		simpMessagingTemplate.convertAndSend("/topic/gauge/data", df.format(d));
		System.out.println("我是DataScheduledTask,getGaugeDataScheduledTask()正在发送后台推送数据："+df.format(d));
	}


}
